from src import app, CustomBlueprint
from src.baseapp.controller.userService.userController import UserController
from flask import Response
import json

from src.baseapp.requestVerify.BaseRequestVerify import *
from src.baseapp.requestVerify.userService.userRequestVerify import *

# 设置这个模块的路由前缀
user_bp = CustomBlueprint('user', __name__, url_prefix='/user')


@user_bp.route('/login', methods=['POST'], header_verify=LoginHeadersSchema, body_verify=LoginBodySchema)
def login(request_obj):
    """
    登录
    headers:
        Content-Type: application/json
    request:
        username: 用户名
        password: <PASSWORD>
    :return:
    """
    username = request_obj.body.username
    password = request_obj.body.password
    with UserController() as user:
        result = user.login(username, password)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@user_bp.route('/user', methods=['GET'], header_verify=HeadersSchema)
def user_info(request_obj):
    token = request_obj.headers.token
    with UserController() as user:
        result = user.get_user_info(token)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@user_bp.route('/login', methods=['DELETE'], header_verify=HeadersSchema)
def logout(request_obj):
    """
    退出登录
    headers:
        token: token
    :return:
    """
    token = request_obj.headers.token
    with UserController() as user:
        result = user.logout(token)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@user_bp.route('/user', methods=['POST'], header_verify=HeadersSchema, body_verify=RegisterBodySchema)
def register(request_obj):
    """
    注册用户
    headers:
        Content-Type: application/json
    request:
        username: 用户名
        password: <PASSWORD>
        email: 邮箱
    :return:
    """
    username = request_obj.body.username
    password = request_obj.body.password
    email = request_obj.body.email
    with UserController() as user:
        result = user.add_user(username, password, email)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@user_bp.route('/resetPassword', methods=['PUT'], header_verify=HeadersSchema, body_verify=ResetPasswordBodySchema)
def reset_password(request_obj):
    """
    重置密码
    headers:
        token: token
        content-type: application/json
    request:
        old_password: 旧密码
        new_password: 新密码
    :return:
    """
    token = request_obj.headers.token
    old_password = request_obj.body.old_password
    new_password = request_obj.body.new_password
    with UserController() as user:
        result = user.reset_password(token, old_password, new_password)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@user_bp.route('/user', methods=['PUT'], header_verify=HeadersSchema, body_verify=UpdateUserBodySchema)
def update_user(request_obj):
    """
    更新用户信息
    headers:
        token: token
        content-type: application/json
    request:
        email: 邮箱
    :return:
    """
    token = request_obj.headers.token
    email = request_obj.body.email
    with UserController() as user:
        result = user.update_user(token, {'email': email})
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@user_bp.route('/user', methods=['DELETE'], header_verify=HeadersSchema, body_verify=DeleteUserBodySchema)
def delete_user(request_obj):
    token = request_obj.headers.token
    user_id = request_obj.body.user_id
    with UserController() as user:
        result = user.delete_user(token, user_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


app.register_blueprint(user_bp)
